package com.tianjs.xdiamandgenerate;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class XDiamandGenerate {

	public static void main(String[] args) {
		if(args.length!=2){
			System.out.println("参数错误");
			return ;
		}
		String sysname = args[0];
		String profile = args[1];
		String currentDir = System.getProperty("user.dir");
		File dir = new File(currentDir);
		File[] files = dir.listFiles();
		BufferedReader bufferedReader = null;
		List<String> contents = new ArrayList<String>();
		for(File file : files){
			if(!file.getName().endsWith(".properties")){
				continue;
			}
			try {
				bufferedReader = new BufferedReader(new FileReader(file));
				String tempString = null;
				while((tempString = bufferedReader.readLine()) != null){
					System.out.println(tempString);
					if(tempString.startsWith("#")
							|| tempString.trim().length()<=0){
						continue;
					}
					String[] tempStringArr = tempString.split("=");
					Entity entity = new Entity(tempStringArr[0],tempStringArr[1],"");
					contents.add(entity.toString()+",");
				}
			} catch (FileNotFoundException e) {
				throw new RuntimeException();
			} catch (IOException e) {
				throw new RuntimeException();
			}finally {
				if(bufferedReader!=null){
					try {
						bufferedReader.close();
					} catch (IOException e) {
						throw new RuntimeException();
					}
				}
			}
		}
		try {
			FileUtils.writeLines(new File(sysname+"_"+profile+".txt"), "UTF-8", contents, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
